// pages/student/period/sign.js

const app = getApp()
var CONFIG = require("../../../config/config.js");
var share = require('../../../utils/share.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        qiname: '',
        showfalse1: false,
        showerca: false,
        id: '', //课程id
        is_status: '', //状态0未结清 1已结清
        choosefalse: false,
        nper_id: '', //报名期次
        nper: '请选择课程班次',
        tel: '',
        status: -1,
        msg: '',
        kind: 1, // 1系列课 2单课
        course_name: '',
        jiaoyanCode: '',
        free_type: '',
        course_id: '',
        uid: '',
        roundlist: [],
        round_id: '',
        showclasssure: false,
        classitemall: '',
        roundname: '',
        user_id: '',
        roundindexnew:'',
        isSave:false
    },
    allnoclass(e) {
        var that = this;
        var index = e.currentTarget.dataset.index;
        var roundlist = that.data.roundlist;
        roundlist[index].noclassfalse = !roundlist[index].noclassfalse;
        that.setData({
            roundlist: that.data.roundlist
        })
    },
    roundchange(e) {
        var that = this;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        var index = e.currentTarget.dataset.index;
        var roundlist = that.data.roundlist;
        that.setData({
            round_index: index,
        })
        if (!roundlist[index].roundfalse) {
            wx.showLoading({
                title: '加载中~',
                mask: true
            })
            that.userCoursesInit(item.id, 2);
        } else {
            roundlist[index].roundfalse = !roundlist[index].roundfalse;
        }
        
        that.setData({
            roundlist: that.data.roundlist
        })
    },
    classchange(e) {
        var that = this;
        var classitem = e.currentTarget.dataset.classitem || e.target.dataset.classitem;
        var item = e.currentTarget.dataset.item || e.target.dataset.item;
        var index = e.currentTarget.dataset.index;
        that.setData({
            round_index:index
        })
        // 未报名
        if (Number(classitem.is_change) <= 0) {
            that.setData({
                roundname: item.name,
                classitemall: classitem,
                showclasssure: true,
            })
        }
    },
    classchangeno() {
        this.setData({
            showclasssure: false,
        })
    },
    majorChange() {
        var that = this;
        var items = that.data.classitemall;

        var is_status = that.data.is_status; //状态0未结清 1已结清
        //1-选班补全费用 2-报道补全费用
        var free_type = items.free_type;
        if (Number(is_status) <= 0 && Number(free_type) == 1) {
            that.setData({
                qiname: items.title,
                showfalse1: true,
                showclasssure: false,
            })
        } else {
            that.setData({
                qiname: items.title,
                nper_id: items.id,
                kind: items.kind,
                free_type: free_type,
            })
            that.baoclick();
        }
    },
    /// 长按
    longTap: function() {
        // console.log("long tap")
        var jiaoyanCode = this.data.jiaoyanCode;
        share.sendImage(jiaoyanCode);
    },
    closeImage() {
        var that = this;
        that.setData({
            showerca: false,
        })
    },
    // 点击不全款
    sureclick1() {
        var that = this;
        that.setData({
            showfalse1: false
        })
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var data = {
            user_course_id: that.data.id,
            uid: that.data.uid,
        }

        share.wxRequest('crmapi/Student/bukuanAll', data, function(res) {
            wx.hideLoading()
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                that.setData({
                    showerca: true,
                    jiaoyanCode: CONFIG.API_PIC + data.qr_img
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 5000,
                })
            }

        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        wx.showLoading({
            title: '加载中~',
            mask: true
        })
        var that = this;
        if (options.stu_id) {
            this.setData({
                id: options.stu_id,
                course_id: options.course_id
            })
        }
        that.round(1)
        if (options.uid) {
            this.setData({
                uid: options.uid,
            })
        }
        if (options.user_id) {
            this.setData({
                user_id: options.user_id,
            })
        }

        var is_status = options.status;
        this.setData({
            is_status: options.status,
            course_name: options.course_name
        });

        wx.setNavigationBarTitle({
            title: '期次报名'
        })

        // 获取本地手机号
        var phone = wx.getStorageSync('tel');

        that.setData({
            tel: phone
        })
    },

    round(istype) {
        var that = this;
        var data = {
            uc_id: that.data.id,
        }
        share.wxRequest('crmapi/Classroom/round', data, function(res) {
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                if (Number(data.length) > 0) {
                    if(Number(istype)!=2){
                        for (var i = 0; i < data.length; i++) {
                            data[i]['roundfalse'] = false;
                            data[i]['noclassfalse'] = false;
                            data[i]['classlist'] = [];
                            data[i]['noclasslist'] = [];
                        }
                        var round_index = 0;
                    }
                    // var round_index1 = that.data.roundindexnew;
                    if(Number(istype)==2){
                        // data[round_index1].roundfalse = true;
                        var round_index = that.data.round_index;
                    }
                    that.setData({
                        roundlist: data,
                        round_index:round_index,
                    })
                    
                    that.userCoursesInit(data[0].id, 1);
                } else {
                    that.setData({
                        status: res.data.errcode,
                        msg: res.data.msg
                    })
                    wx.hideLoading();
                }
            } else {
                that.setData({
                    status: res.data.errcode,
                    msg: res.data.msg
                })
                wx.hideLoading();
            }
        })
    },

    userCoursesInit(round_id, type) {
        let that = this;
        var data = {
            uc_id: that.data.id,
            round_id: round_id
        }
        share.wxRequest('crmapi/Classroom/classNew', data, function(res) {
            wx.hideLoading();
            that.setData({
                status: res.data.errcode,
                msg: res.data.msg
            })
            if (Number(res.data.errcode) == 0) {
                var data = res.data.data;
                var roundlist = that.data.roundlist;
                if (Number(data.length) > 0) {
                    var round_index = that.data.round_index;
                    roundlist[round_index].classlist = [];
                    roundlist[round_index].noclasslist = [];
                    for (var i = 0; i < data.length; i++) {
                        if (Number(data[i].is_can) <= 0) {
                            roundlist[round_index].classlist.push(data[i])
                        } else {
                            if (Number(data[i].is_change) == 1) {
                                roundlist[round_index].classlist.push(data[i])
                            } else {
                                roundlist[round_index].noclasslist.push(data[i])
                            }
                        }
                    }
                    if (Number(type) == 2) {
                        roundlist[round_index].roundfalse = true
                    }
                    // is_can 0可选 1不可选
                    that.setData({
                        roundlist: roundlist
                    })
                }
            }
        })
    },

    // 点击报名
    baoclick: function() {
        let that = this;
        var is_status = that.data.is_status;
        var free_type = that.data.free_type;
        if (Number(that.data.nper_id) == 0 || that.data.nper_id == '') {
            wx.showToast({
                title: '请选择课程期次',
                icon: 'none'
            })
        } else if (Number(is_status) <= 0 && Number(free_type) == 1) {
            that.setData({
                showfalse1: true
            })
        } else {
            wx.showLoading({
                title: '加载中~',
            })
            var data = {
                uc_id: that.data.id,
                nper_id: that.data.nper_id,
                kind: that.data.kind,
            }
            that.setData({
                isSave: true
            })
            share.wxRequest('crmapi/Classroom/classStudentNew', data, function(res) {
                wx.hideLoading();
                that.setData({
                    isSave: false
                })
                if (Number(res.data.errcode) == 0) {
                    var roundlist = that.data.roundlist;
                    var round_index = that.data.round_index;
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 4000,
                    })
                    that.setData({
                        showclasssure: false,
                    })
                    roundlist[round_index].is_change = 1;
                    that.userCoursesInit(roundlist[round_index].id,2);
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 4000,
                    })
                }
            })
        }
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        var phone = wx.getStorageSync('tel');
        // console.log(phone);
        that.setData({
            tel: phone
        })
    },
})